package com.mtiiot.fstcpserver.mq.kafka.pool;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

/**
 * Created by xyr on 2016/7/26.
 */
public class CreateKafkaProducerPool {
    public CreateKafkaProducerPool() {
    }

    public GenericObjectPool<KafkaProducerApp> createKafkaProducerPool() {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        short maxNumProducers = 200;
        byte minNumProducers = 20;
        poolConfig.setMaxTotal(maxNumProducers);
        poolConfig.setMaxIdle(maxNumProducers);
        poolConfig.setMaxWaitMillis(5000L);
        poolConfig.setMinIdle(minNumProducers);
        KafkaProducerAppFactory factory = new KafkaProducerAppFactory();
        PooledKafkaProducerAppFactory pooledProducerFactory = new PooledKafkaProducerAppFactory(factory);
        return new GenericObjectPool(pooledProducerFactory, poolConfig);
    }
}
